<?php

declare(strict_types=1);


namespace App\Controller\Api;

use App\Controller\AbstractController;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Di\Annotation\Inject;
use App\Utils\ApiUtils;
use Hyperf\Utils\Context;
use App\Validate\PhoneValidation;
use App\Service\IndexApiService;
use App\Service\CouponService;
use App\Service\AmountService;
use App\Constants\ErrorCode;
use App\Validate\IndexapiValidation;

class IndexController extends AbstractController
{

    /**
     * @Inject 
     * @var IndexApiService
     */
    protected $service;

    /**
     * @Inject 
     * @var CouponService
     */
    protected $couponService;

    /**
     * @Inject 
     * @var AmountService
     */
    protected $amountService;

    /**
     * czm
     * 小程序入口 以后可能做接口集合用
     */
    public function index()
    {
        return 'hello';
    }

    public function mobileOperator()
    {
        $phone = $this->request->input('phone');
        make(PhoneValidation::class)->check(['phone'=>$phone],'phone')->response();
        $result = $this->service->phone($phone);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$result));
    }

    /**
     * 首页-返回充值类型数据
     * czm
     */
    public function rechargeType()
    {
        $data = $this->amountService->typeList();
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $data));
       
    }

    /**
     * 首页-返回充值金额列表
     */
    public function amount()
    {
        $type_id = (int) $this->request->input('type_id', 1);
        $params['where']=['status'=> 1,'type_id'=>$type_id];
        $params['fields']=['amount','ori_amount', 'type_id', 'status'];
        $params['orderBy']='ori_amount';
        $data=$this->amountService->amount($params);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $data));
    }

    /**
     * 首页-返回油卡充值金额列表
     */
    public function oilAmount()
    {
        $type_id = (int) $this->request->input('type_id', 1);
        $params['where']=['status'=> 1,'type_id'=>$type_id];
        $params['fields']=['amount','ori_amount', 'type_id', 'status','name','item_id'];
        $params['orderBy']='ori_amount';
        $data=$this->amountService->oil($params);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $data));
    }
 

    /**
     * 首页-返回判断用户是否有可用红包
     * czm
     */
    public function isHasCoupon()
    {
        $jwt = Context::get('jwt_token');
        $data = $this->couponService->isHasCoupon($jwt['account']);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $data));
    }

    /**
     * 首页-获取用户可用红包信息
     * czm
     */
    public function userCoupon()
    {
        $jwt = Context::get('jwt_token');
        $data = $this->couponService->getUserCoupon($jwt['account'], 2);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $data));
    }

    /**
     * 首页-我的红包-列表
     * czm
     */
    public function userCouponAll()
    {
        $jwt = Context::get('jwt_token');
        $data = $this->couponService->getUserCoupon($jwt['account'], 1);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $data));
    }

    /** 图片上传插件
     * @Author abell
     * @DateTime 2020-05-28
     * @param string $request
     * @return object
     */
    public function uploadImg(RequestInterface $request)
    {
        if ($request->hasFile('img')) {
            return  $this->jsonApi($this->service->uploadImg($request->file('img')));
        } else {
            return $this->jsonApi(['code'=>ErrorCode::ERR_SERVER,'msg'=>ErrorCode::getMessage(ErrorCode::ERR_SERVER),'data'=>'']);
        }
    }

    /** 远程图片下载上传接口
     * @Author abell
     * @DateTime 2020-05-28
     * @param string $request
     * @return object
     */
    public function downloadImg(RequestInterface $request)
    {
        make(IndexapiValidation::class)->check($request->all(),'download_img')->response();
        return $this->jsonApi($this->service->downloadImg($request->input('imgurl')));
    }
}
